/*
 *  Copyright (C) 2010 Jonathan MERCIER <bioinfornatics@fedoraproject.org>,
 *  Louise-Amélie SCHMITT <louise-amelie.schmitt@etu.u-bordeaux1.fr>,
 *  Florence Maurier <florence_maurier@orange.fr>,
 *  Marianne Dussart <marianne.dussart@orange.fr>,
 *  Nicolas Fontaine <nifontaine@gmail.com>
 * 
 *  JACoMode is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  JACoMode is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with JACoMode.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.ubdx1.jacomode.models;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.ArrayList;

/**
 *
 * @author Jonathan MERCIER <bioinfornatics@fedoraproject.org>,Louise-Amélie SCHMITT <louise-amelie.schmitt@etu.u-bordeaux1.fr>,Florence Maurier <florence_maurier@orange.fr>,Marianne Dussart <marianne.dussart@orange.fr>,Nicolas Fontaine <nifontaine@gmail.com>
 */
public class InputData
{
    /* Attributes */
    public ArrayList<String> datList = new ArrayList<String>();

    public ArrayList<String> doutList = new ArrayList<String>();

    public String pathDat;

    public String pathOut;

    public String pathDatOut;

    public String pathOutOut;

    /**
     * Takes the paths of the files.
     * @param pathDat source dat path
     * @param pathOut source out path
     * @param pathDatOut result dat path
     * @param pathOutOut result out path
     */
    public InputData(String pathDat,String pathOut,String pathDatOut, String pathOutOut)
    {
        this.pathDat    = pathDat;
        this.pathOut    = pathOut;
        this.pathDatOut = pathDatOut;
        this.pathOutOut = pathOutOut;


        try
        {
            parseDat();
        }
        catch(IOException error)
        {
            error.printStackTrace();
        }

        try
        {
            parseOut();
        }
        catch(IOException error)
        {
            error.printStackTrace();
        }

    }

    /**
     * Analyzes the OUT result files.
     * @throws IOException if an I/O error occurs
     */
    public void parseOut() throws IOException
    {
        Scanner     s       = null;
        File        file    = new File(pathOutOut);
        boolean     balise  = false;
        String      mot1    = "ELEMENTARY MODES";
        String      mot2    = "enzymes";
        String      motinter= "";
        String      bl      = "";
        Pattern     p1      = Pattern.compile(mot1);
        Pattern     p2      = Pattern.compile(mot2);
        FileWriter  fw;

        try
        {
            s = new Scanner(new BufferedReader(new FileReader(pathOut)));
        }
        finally
        {
            while (s.hasNextLine())
            {
                bl          = "";
                bl          = s.nextLine();
                Matcher m1  = p1.matcher(bl);
                boolean b1  = m1.matches();
                Matcher m2  = p2.matcher(bl);
                boolean b2  = m2.matches();		             
                if (b1)
                {
                    balise=true;
                    
                }
                if (b2)
                {
                    balise=false;
                    
                }
                if (balise==true)
                {
                    doutList.add(bl);
                }        

            }
            if (s != null)
            {
                s.close();
            }
        }
        doutList.remove(0);
        doutList.remove(0);
        doutList.remove(0);
		doutList.remove(doutList.size()-1);
		doutList.remove(doutList.size()-1);
		doutList.remove(doutList.size()-1);
		doutList.remove(doutList.size()-1);
        fw = new FileWriter(file);
        for (int k = 0; k < doutList.size(); k++)
        {
            String retourligne=doutList.get(k)+"\n";
            //On ecrit la chaine
            fw.write(retourligne);
            //On ferme le flux
        }
        fw.close();
    }

    /**
     * Returns an ArrayList containing informations from the OUT file.
     * @return an ArrayList containing informations from the OUT file
     */
    public ArrayList<String> getdoutList()
    {
		return doutList;
	}

    /**
     * Analyzes the DAT result file.
     * @throws IOException if an I/O error occurs
     */
    public void parseDat() throws IOException
    {
        Scanner s       = null;
        File file1      = new File(pathDatOut);
        boolean balise  = false;
        FileWriter fw1;
        String enzrev   = "";
        String enzirev  = "";
        String mot1     = "-ENZREV";
        String mot2     = "-METINT";
        String mot3     = "-ENZIRREV";
        Pattern p1      = Pattern.compile(mot1);
        Pattern p2      = Pattern.compile(mot2);
        Pattern p3      = Pattern.compile(mot3);
        String bl       = "";
        try
        {
            s = new Scanner(new BufferedReader(new FileReader(pathDat)));
            try
            {
                while (s.hasNext())
                {
                    bl          = s.next();
                    Matcher m1  = p1.matcher(bl);
                    boolean b1  = m1.matches();
                    Matcher m2  = p2.matcher(bl);

                    boolean b2 = m2.matches();
                    Matcher m3 = p3.matcher(bl);
                    boolean b3 = m3.matches();
                    if (b1)
                    {
                        balise=true;
                        enzrev=bl;
                    }
                    if (b2)
                    {
                        balise=false;
                    }
                    if (b3)
                    {
                        enzirev=bl;
                    }
                    if (balise)
                    {
                        if (!bl.equals(enzrev) && (!bl.equals(enzirev)))
                            datList.add(bl);
                    }
                }
            }
            finally
            {
                s.close();
            }
        }
        catch(IOException error)
        {
            error.printStackTrace();
        }
        fw1 = new FileWriter(file1);
        for (String string : getdatList())
        {
            String str = string;
            str = string+" ,";
            //the string is written
            fw1.write(str);
            //the stream is closed
	    }
            fw1.close();
    }

    /**
     * Returns an ArrayList containing informations from the OUT file.
     * @return an ArrayList containing informations from the OUT file
     */
    public ArrayList<String> getdatList()
    {
		return datList;
    }

    /**
     * Returns the size of the list containing informations from the OUT file.
     * @return the size of the list containing informations from the OUT file
     */
    public int getdoutListsize()
    {
		return doutList.size();
    }
}
